using System;
using Core.Extensions;


namespace SolarDevices
{
    public class ClassicLookupTables
    {
        static readonly Tupple[] BatteryStage =
            new[]{
                new Tupple { Description = "(Off) No power, waiting for power source, battery voltage over set point.", Value = 0 },
                new Tupple { Description = "(Absorb) Regulating battery voltage at absorb set point until the batteries are charged.", Value = 3 },
                new Tupple { Description = "(Bulk) Max power point tracking until absorb voltage reached.", Value = 4 },
                new Tupple { Description = "(Float) Battery is full and regulating battery voltage at float set point.", Value = 5 },
                new Tupple { Description = "(Float) Max power point tracking. Seeking float set point voltage.", Value = 6 },
                new Tupple { Description = "(Equalize) Regulating battery voltage at equalize set point.", Value = 7 },
                new Tupple { Description = "(Error) Input voltage is above maximum classic operating voltage.", Value = 10 },
                new Tupple { Description = "(Equalizing) Max power point tracking. Seeking equalize set point voltage.", Value = 18 }
            };

        static readonly Tupple[] InfoMap =
            new[]{
                new Tupple { Description = "Over Temperature", Value = 0x00000001 },
                new Tupple { Description = "EEprom error", Value = 0x00000002 },
                new Tupple { Description = "Equalize charge state active", Value = 0x00000008 },
                new Tupple { Description = "Battery voltage is less than EQ voltage", Value = 0x00000080 },
                new Tupple { Description = "PV voltage is less than VBatt", Value = 0x00000100 },
                new Tupple { Description = "Current limit reached", Value = 0x00000200 },
                new Tupple { Description = "Hyper Voc PV input is between Vmax and Vmax+VBatt", Value = 0x00000400 },
                //new Tupple { Description = "Battery sensor installed", Value = 0x00002000 },
                new Tupple { Description = "Aux 1 state on", Value = 0x00004000 },
                new Tupple { Description = "Aux 2 state on", Value = 0x00008000 },
                new Tupple { Description = "Ground fault detected", Value = 0x00010000 },
                new Tupple { Description = "FET drive error", Value = 0x00020000 },
                new Tupple { Description = "Arc fault occured", Value = 0x00040000 },
                new Tupple { Description = "Negative battery current", Value = 0x00080000 },
                new Tupple { Description = "FET error", Value = 0x00100000 },
                new Tupple { Description = "Very low battery", Value = 0x01000000 },
                //new Tupple { Description = "Stack jumper not installed", Value = 0x02000000 },
                new Tupple { Description = "EQ finished", Value = 0x04000000 },
                new Tupple { Description = "Temperature Comp shorted", Value = 0x80000000 }
                //new Tupple { Description = "Lock jumper is not installed", Value = 0x10000000 },
                //new Tupple { Description = "Extra jumper is not installed", Value = 0x20000000 }
            };

            public static string ParseInfoFlag(UInt32 infoFlag)
            {
                var b = new StringBuilder("");
                bool first = true;
                foreach (var infoItem in InfoMap)
                {
                    var bit = infoFlag & infoItem.Value;
                    if (bit != 0)
                    {
                        if (!first)
                        {
                            b.Append(" - ");
                        }
                        else
                        {
                            first = false;
                        }
                        b.Append(infoItem.Description);
                    }
                }
                return b.ToString();
            }

            public static string LookupBatteryStage(int val)
            {
                foreach (var infoMap in BatteryStage)
                {
                    if (val == infoMap.Value)
                    {
                        return infoMap.Description;
                    }
                }
                return "Undefined";
            }

        }
}
